package com.qingfeng.medical.controller.admin.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.handler.GeneralAuthException;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.order.domain.entity.VisitRecord;
import com.qingfeng.medical.biz.order.domain.enums.VisitStatusEnum;
import com.qingfeng.medical.biz.order.service.VisitRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户就诊记录
 *
 * @author 清风学Java
 * @date 2023-05-15 17:21:51
 */
@Api(tags = "用户就诊记录管理")
@RestController
@RequestMapping("/admin/info/visitRecord")
@CrossOrigin
@Validated
public class VisitRecordController extends BaseController {

    @Autowired
    private VisitRecordService visitRecordService;

    @ApiOperation(value = "根据订单Id查询用户就诊记录信息", notes = "根据订单Id查询用户就诊记录信息")
    @GetMapping("/info/{id}")
    @Log(title = "用户就诊记录管理", businessType = BusinessType.SELECT, message = "根据订单Id查询用户就诊记录信息")
    public Result visitInfo(@PathVariable("id") Long id) {
        return Result.ok(visitRecordService.getOne(
                new LambdaQueryWrapper<VisitRecord>()
                        .eq(VisitRecord::getOrderId, id)
        ));
    }

    @ApiOperation(value = "一键就诊", notes = "一键就诊")
    @PutMapping("/{orderId}")
    @Log(title = "用户就诊记录管理", businessType = BusinessType.SELECT, message = "根据订单Id查询用户就诊记录信息")
    @Transactional(rollbackFor = GeneralAuthException.class)
    public Result updateVisitInfo(@PathVariable("orderId") Long orderId){
        VisitRecord visitRecord = visitRecordService.getOne(
                new LambdaQueryWrapper<VisitRecord>()
                        .eq(VisitRecord::getOrderId, orderId)
        );
        visitRecord.setVisitStatus(VisitStatusEnum.SUCCESS);
        visitRecordService.updateById(visitRecord);
        return Result.ok();
    }

}
